﻿using System;
using System.Configuration;
using System.Runtime.Remoting.Metadata.W3cXsd2001;
using System.Windows.Forms;
using Communergy.Common;
using Communergy.Powermeter;
using Communergy.Repository;
using CommunergyUploader.Exporters;
using CommunergyUploader.Presenters;
using Microsoft.Practices.Unity;

namespace CurrentCostGUI
{
    public partial class CommunergyMDIParent : Form
    {
        private int childFormNumber = 0;

        public CommunergyMDIParent()
        {
            InitializeComponent();
        }

        private void ShowNewForm(object sender, EventArgs e)
        {

            var newMeterForm = new AddNewMeter();
            newMeterForm.MdiParent = this;
            newMeterForm.Show();
        }

        private void OpenFile(object sender, EventArgs e)
        {

            var selectMeter = new SelectMeter();
            var result = selectMeter.ShowDialog();
            if (result == DialogResult.OK)
            {
                var selectedMeter = selectMeter.SelectedMeter;

                //string connectionString = ConfigurationHelper.SqliteConnectionString(ConfigurationHelper.SQLLitePath);
                using (IUnityContainer container = new UnityContainer())
                {
                    container.RegisterType<IDataExportCommandRepository, LocalDataExportCommandRepository>();
                    container.RegisterType<IMeterView, Meter>();
                    container.RegisterType<IRepositoryFactory, SQLiteRepositoryFactory>();

                    //TODO - make meter a property of IDataExportCommandRepository
                    container.Configure<InjectedMembers>()
                        .ConfigureInjectionFor<LocalDataExportCommandRepository>(
                            new InjectionConstructor(selectedMeter));
                    
                    MeterPresenter meterPresenter = container.Resolve<MeterPresenter>();

                    Meter meterForm = (Meter)meterPresenter.View;
                    meterForm.MdiParent = this;
                    meterForm.Show();
                    meterForm.BindMeterToForm(selectedMeter);
                }
            }
            else
            {
               
            }

        }

        private void SaveAsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SaveFileDialog saveFileDialog = new SaveFileDialog();
            saveFileDialog.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
            saveFileDialog.Filter = "Text Files (*.txt)|*.txt|All Files (*.*)|*.*";
            if (saveFileDialog.ShowDialog(this) == DialogResult.OK)
            {
                string FileName = saveFileDialog.FileName;
            }
        }

        private void ExitToolsStripMenuItem_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void CutToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void CopyToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void PasteToolStripMenuItem_Click(object sender, EventArgs e)
        {
        }

        private void ToolBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            toolStrip.Visible = toolBarToolStripMenuItem.Checked;
        }

        private void StatusBarToolStripMenuItem_Click(object sender, EventArgs e)
        {
            statusStrip.Visible = statusBarToolStripMenuItem.Checked;
        }

        private void CascadeToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.Cascade);
        }

        private void TileVerticalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileVertical);
        }

        private void TileHorizontalToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.TileHorizontal);
        }

        private void ArrangeIconsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            LayoutMdi(MdiLayout.ArrangeIcons);
        }

        private void CloseAllToolStripMenuItem_Click(object sender, EventArgs e)
        {
            foreach (Form childForm in MdiChildren)
            {
                childForm.Close();
            }
        }

        private void optionsToolStripMenuItem_Click(object sender, EventArgs e)
        {
            ConfigForm configForm = new ConfigForm();
           // configForm.MdiParent = this;

            configForm.ShowDialog();

        }

        private void synchroniseMetersToolStripMenuItem_Click(object sender, EventArgs e)
        {
            SynchroniseMeters metersForm = new SynchroniseMeters();
            metersForm.MdiParent = this;
            metersForm.Show();
        }
    }
}
